﻿@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Index.cshtml";
}
<script>
    const levelMap = {
        "Ⅲ级": "level3",
        "Ⅱ级": "level2",
        "Ⅰ级": "level1"
    };

    $(function () {
        gridList();
    })
    // Routecode 转换函数
    function convertRouteCode(rawCode) {
        try {
            // 尝试直接映射（如果原始数据已经是 "Ⅲ级"）
            if (levelMap[rawCode]) {
                return levelMap[rawCode];
            }

            // 如果原始数据是乱码（如编码问题），尝试手动修复
            // 示例：假设乱码表现为 "Ⅲ级" 显示为 "%E2%85%A2%E7%BA%A7"
            const fixedCode = decodeURIComponent(rawCode); // 尝试 URL 解码
            return levelMap[fixedCode] || fixedCode; // 优先使用映射，否则返回解码后的值
        } catch (e) {
            console.error('Routecode 转换失败:', rawCode, e);
            return rawCode; // 转换失败时返回原始值
        }
    }
         function gridList() {
        var $gridList = $("#gridList");
        $gridList.dataGrid({
            url: "/SystemSecurity/BridgeMange/GetGridJson",
            height: $(window).height() - 96,
            colModel: [
                { label: "桥梁名称", name: "Bridgetype",width:117, key: true },
                { label: '桥梁代码', name: 'Bridgecode', width: 150, align: 'left' },
                { label: '中心桩号', name: 'Centercode', width: 100, align: 'left' },
                { label: '检查频率', name: 'Routecode', width: 50, align: 'middle' },
                { label: '  路线号', name: 'Routename', width: 80, align: 'left' },
                { label: '路线名称', name: 'Routelevel', width: 50, align: 'left' },
                { label: '路线等级', name: 'Bridgelength', width: 100, align: 'left' },
                { label: '跨径分类类型', name: 'KyueLandType', width: 180, align: 'left' },
                {
                    label: '检查日期',
                    name: 'CheckTime',
                    width: 100,
                    align: 'left',
                    // 自定义格式化函数：处理 "未检查" 或日期字符串
                    formatter: function (cellValue) {
                        if (cellValue === '未检查' || !cellValue) {
                            return '暂未检查';
                        } else {
                            return cellValue.split(' ')[0]; // 输出 '2023-10-01'
                        }
                    }
                },
                {
                    label: '下次检查日期',
                    name: 'CheckNextTime',
                    width: 100,
                    align: 'left',
                    // 同上
                    formatter: function (cellValue) {
                        if (cellValue === '未检查' || !cellValue) {
                            return '暂未检查';
                        } else {
                            return cellValue.split(' ')[0];
                        }
                    }
                },
                { label: '检查报告', hidden: true, name: 'CheckPort', width: 100, align: 'left' },
                { label: '定检承担单位', hidden: true, name: 'DjProcedureCompany', width: 50, align: 'middle' },
              
               
            ],
            ondblClickRow: function (rowid, iRow, iCol, e) {
                var rowData = $(this).jqGrid('getRowData', rowid);
                var rawRouteCode = rowData.Routecode;
                var rawCheckPort = rowData.CheckPort ?? "";    
                var rawDjProcedureCompany = rowData.DjProcedureCompany ?? "";
                var routeCode = convertRouteCode(rawRouteCode);
                var keyValue = encodeURIComponent(rowData.Bridgecode || ''); //ID
                var RouteCode1 = encodeURIComponent(routeCode || ''); // 确保字段名正确
                if (rawCheckPort == "" || rawDjProcedureCompany == "") {
                    rawCheckPort = "Nodata";
                    rawDjProcedureCompany = "Nodata";
                }
                console.log('频率', RouteCode1)
                console.log('rawCheckPort', rawCheckPort)
                console.log('rawDjProcedureCompany', rawDjProcedureCompany)

                $.modalOpen({
                    id: "Details",
                    title: "桥梁定检记录添加",
                    url: "/SystemSecurity/BridgeMange/Details?keyValue=" + keyValue +
                        "&RouteCode=" + RouteCode1 + "&rawCheckPort=" + rawCheckPort + "&rawDjProcedureCompany=" + rawDjProcedureCompany, // 正确使用&连接参数
                    width: "450px",
                    height: "520px",
                    btn: null,
                });
            }
        });
        $("#txt_condition .dropdown-menu li").click(function () {
            var text = $(this).find('a').html();
            var value = $(this).find('a').attr('data-value');
            $("#txt_condition .dropdown-text").html(text).attr('data-value', value)
        });
        $("#btn_search").click(function () {
            var queryJson = {
                condition: $("#txt_condition").find('.dropdown-text').attr('data-value'),
                keyword: $("#txt_keyword").val()
            }
            $gridList.jqGrid('setGridParam', {
                postData: { queryJson: JSON.stringify(queryJson) },
            }).trigger('reloadGrid');
        });
    }

        $("#txt_condition .dropdown-menu li").click(function () {
            var text = $(this).find('a').html();
            var value = $(this).find('a').attr('data-value');
            $("#txt_condition .dropdown-text").html(text).attr('data-value', value)
        });
        $("#btn_search").click(function () {
            var queryJson = {
                condition: $("#txt_condition").find('.dropdown-text').attr('data-value'),
                keyword: $("#txt_keyword").val()
            }
            $gridList.jqGrid('setGridParam', {
                postData: { queryJson: JSON.stringify(queryJson) },
            }).trigger('reloadGrid');
        });
    
    function btn_add() {
        $.modalOpen({
            id: "Form",
            title: "新增备份",
            url: "/SystemSecurity/DbBackup/Form",
            width: "450px",
            height: "360px",
            callBack: function (iframeId) {
                top.frames[iframeId].submitForm();
            }
        });
    }
    function btn_delete() {
        $.deleteForm({
            url: "/SystemSecurity/DbBackup/DeleteForm",
            param: { keyValue: $("#gridList").jqGridRowValue().F_Id },
            success: function () {
                $.currentWindow().$("#gridList").trigger("reloadGrid");
            }
        })
    }
    function btn_download() {
        // 获取选中的行ID
        var rowid = $("#gridList").jqGrid('getGridParam', 'selrow'); 
        console.log('row id:',rowid)
        if (!rowid) {
            alert("请选择一行数据");
            return;
        }
        var rowData = $("#gridList").jqGrid('getRowData', rowid);
        console.log('rowData:', rowData)
            var rawRouteCode = rowData.Routecode;
            var rawCheckPort = rowData.CheckPort ?? "";
            var rawDjProcedureCompany = rowData.DjProcedureCompany ?? "";
            var routeCode = convertRouteCode(rawRouteCode);
            var keyValue = encodeURIComponent(rowData.Bridgecode || ''); //ID
            var RouteCode1 = encodeURIComponent(routeCode || ''); // 确保字段名正确
            if (rawCheckPort == "" || rawDjProcedureCompany == "") {
                rawCheckPort = "Nodata";
                rawDjProcedureCompany = "Nodata";
            }
            console.log('频率', RouteCode1)
            console.log('rawCheckPort', rawCheckPort)
            console.log('rawDjProcedureCompany', rawDjProcedureCompany)

            $.modalOpen({
                id: "Details",
                title: "桥梁定检记录",
                url: "/SystemSecurity/BridgeMange/Details?keyValue=" + keyValue +
                    "&RouteCode=" + RouteCode1 + "&rawCheckPort=" + rawCheckPort + "&rawDjProcedureCompany=" + rawDjProcedureCompany + "&view=" + '123', // 正确使用&连接参数
                width: "1000px",
                height: "900px",
                btn: null,
            });
        
    }
    function btn_delete() {
        // 获取选中的行ID
        var rowid = $("#gridList").jqGrid('getGridParam', 'selrow');
        console.log('row id:', rowid)
        if (!rowid) {
            alert("请选择一行数据");
            return;
        }
        var rowData = $("#gridList").jqGrid('getRowData', rowid);
        console.log('rowData:', rowData)
        var rawRouteCode = rowData.Routecode;
        var rawCheckPort = rowData.CheckPort ?? "";
        var rawDjProcedureCompany = rowData.DjProcedureCompany ?? "";
        var routeCode = convertRouteCode(rawRouteCode);
        var keyValue = encodeURIComponent(rowData.Bridgecode || ''); //ID
        var RouteCode1 = encodeURIComponent(routeCode || ''); // 确保字段名正确
        if (rawCheckPort == "" || rawDjProcedureCompany == "") {
            rawCheckPort = "Nodata";
            rawDjProcedureCompany = "Nodata";
        }
        console.log('频率', RouteCode1)
        console.log('rawCheckPort', rawCheckPort)
        console.log('rawDjProcedureCompany', rawDjProcedureCompany)

        $.modalOpen({
            id: "Details",
            title: "桥梁病害记录",
            url: "/SystemSecurity/BridgeMange/Details?keyValue=" + keyValue +
                "&RouteCode=" + RouteCode1 + "&rawCheckPort=" + rawCheckPort + "&rawDjProcedureCompany=" + rawDjProcedureCompany + "&view=" + 'disease', // 正确使用&连接参数
            width: "1000px",
            height: "900px",
            btn: null,
        });

    }
</script>

<div class="topPanel">
    <div class="toolbar">
        <div class="btn-group">
            <a class="btn btn-primary" onclick="$.reload()"><span class="glyphicon glyphicon-refresh"></span></a>
        </div>
        <div class="btn-group">
            <a id="NF-add" authorize="yes" class="btn btn-primary dropdown-text" onclick="btn_add()"><i class="fa fa-plus"></i>新建备份</a>
        </div>
        <div class="operate">
            <ul class="nav nav-pills">
                <li class="first">已选中<span>1</span>项</li>
                <li><a id="NF-delete" authorize="yes" onclick="btn_delete()"><i class="fa fa-cloud-download"></i>病害记录添加</a></li>
                <li class="split"></li>
                <li><a id="NF-download" authorize="yes" onclick="btn_download()"><i class="fa fa-cloud-download"></i>定检记录添加</a></li>
            </ul>
            <a href="javascript:;" class="close"></a>
        </div>
        <script></script>
    </div>
    <div class="search">
        <table>
            <tr>
                <td style="padding-left: 2px;">
                    <div class="input-group">
                        <input id="txt_keyword" type="text" class="form-control" placeholder="请输入要查询关键字" style="width: 200px;">
                        <span class="input-group-btn">
                            <button id="btn_search" type="button" class="btn  btn-primary"><i class="fa fa-search"></i></button>
                        </span>
                    </div>
                </td>
            </tr>
        </table>
    </div>
</div>
<div class="gridPanel">
    <table id="gridList"></table>
</div>
